package com.zdx.yswl.ui.tool;

import java.net.URISyntaxException;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.zdx.yswl.R;
import com.zdx.yswl.base.KActivity;
import com.zdx.yswl.tool.KLog;
import com.zdx.yswl.ui.activity.MapActivity;

/**
 * 百度 地理编码功能 
 * 传入 省份名称（必要），  详细地址 （必要），地名  
 * 有时候 偏远地区定位不准确
 * 需要 补充参数 经纬度
 *
 */

@Deprecated
public class BaseMapActivity extends KActivity implements OnMarkerClickListener, OnGetGeoCoderResultListener {
	protected MapView mapView;
	private BaiduMap mBaiduMap;
	private GeoCoder mSearch;
	protected LatLng mLatLng; // 从地址信息 转换得来
	private LatLng mLa;// 外部传入经纬度参数
	private boolean tag = true;
	protected Marker mMarkerA;
	PoiSearch mPoiSearch;

	private String address = "虹梅南路1755号";
	private String city = "上海";
	private String addressName = "伊诺尔集团";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		Intent intent = getIntent();
		 city = intent.getStringExtra("city");
		 address = intent.getStringExtra("address");
		 addressName = intent.getStringExtra("addressName");
		 mLa = new LatLng(Double.valueOf(intent.getStringExtra("latitude")), Double.valueOf(intent.getStringExtra("longitude")));

		initView();
	}

	private void initView() {
		mapView = (MapView) super.findViewById(R.id.bmapsView);
		ImageButton back = (ImageButton) super.findViewById(R.id.map_nav_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		Button navBtn = (Button) super.findViewById(R.id.daohang);
		navBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				openBd();
			}
		});
		mBaiduMap = (BaiduMap) mapView.getMap();
		mBaiduMap.setOnMarkerClickListener(this);

		// TODO:先从数据库中取历史数据，如果没有再进行搜索
		mSearch = GeoCoder.newInstance();
		mSearch.setOnGetGeoCodeResultListener(this);
		mSearch.geocode(new GeoCodeOption().city(this.city).address(this.address));

	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		openBd();
		return false;
	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult __result) {
		if (tag) {
			if (__result == null || __result.error != SearchResult.ERRORNO.NO_ERROR) {
				Toast.makeText(this, "抱歉，未能找到结果", Toast.LENGTH_LONG).show();
			} else {
				mBaiduMap.clear();
				mLatLng = __result.getLocation();
				KLog.e("BaiduMap", "----维度" + mLatLng.latitude + "----经度" + mLatLng.longitude);
				MarkerOptions __markerOptions = new MarkerOptions();
				__markerOptions = __markerOptions.position(mLatLng);
				__markerOptions = __markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka));
				__markerOptions = __markerOptions.title(address);
				mMarkerA = (Marker) mBaiduMap.addOverlay(__markerOptions);
				mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(mLatLng));

//				 Button tagTv = new Button(getApplicationContext());
//				 tagTv.setGravity(Gravity.CENTER);
//				 tagTv.setBackgroundResource(R.drawable.map_nav);
//				 tagTv.setText(addressName+" ");
				//
				// final LatLng ll = mMarkerA.getPosition();
				// Point p = mBaiduMap.getProjection().toScreenLocation(ll);
				// p.y -= 47;
				// LatLng llInfo =  mBaiduMap.getProjection().fromScreenLocation(p);
				// InfoWindow mInfoWindow = new InfoWindow(tagTv, llInfo, null);
				// mBaiduMap.showInfoWindow(mInfoWindow);

			}
		} else { // 当外部传入参数为 经纬度 时 执行此段代码
			MarkerOptions __markerOptions = new MarkerOptions();
			__markerOptions = __markerOptions.position(mLa);
			__markerOptions = __markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka));
			mMarkerA = (Marker) mBaiduMap.addOverlay(__markerOptions);
			mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(mLa));
		}
	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult __result) {
	}

	@Override
	public void onDestroy() {
		mSearch.destroy();
		mapView.onDestroy();
		super.onDestroy();
	}

	@Override
	public void onPause() {
		mapView.onPause();
		super.onPause();
	}

	@Override
	public void onResume() {
		mapView.onResume();
		super.onResume();
	}

	/**
	 * 
	 * @param context
	 * @param city
	 *            城市
	 * @param address
	 *            街道地址
	 * @param addressName
	 *            名字
	 * @return
	 */
	// public static Intent actionStart(Context context, String city, String
	// address, String addressName) {
	// Intent intent = new Intent(context, MapActivity.class);
	// intent.putExtra("city", city);
	// intent.putExtra("address", address);
	// intent.putExtra("addressName", addressName);
	// return intent;
	// }
	public static Intent actionStart(Context context, String latitude, String longitude) {
		Intent intent = new Intent(context, MapActivity.class);
		intent.putExtra("latitude", latitude);
		intent.putExtra("longitude", longitude);
		return intent;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return true;
	}

	private void openBd() {
		if (mLatLng == null) {
			return;
		}
		KLog.e("BaiduMap", "----维度" + mLatLng.latitude + "----经度" + mLatLng.longitude);
		MapActivity.actionMap(BaseMapActivity.this, mLatLng.latitude + "", mLatLng.longitude + "", address, addressName);
	}

	/**
	 * 
	 * @param context
	 * @param latitude
	 *            维度
	 * @param longitude
	 *            经度
	 * @param address
	 *            街道详细地址
	 * @param addressName
	 *            公司名字
	 */
	public static void actionMap(Context context, String latitude, String longitude, String address, String addressName) {
		String appName = context.getResources().getString(R.string.app_name);
		try {

			String requestUrl = "intent://map/marker?location=" + latitude + "," + longitude + "&title="//
					+ address + "&content=" + addressName + "&src=" //
					+ appName + "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end"; //
			context.startActivity(Intent.getIntent(requestUrl));

		} catch (BaiduMapAppNotSupportNaviException e) {
			Uri uri = Uri.parse("geo:" + latitude + "," + longitude + "?q=" + addressName);
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			context.startActivity(intent);
		} catch (URISyntaxException e) {
			Toast.makeText(context, "手机无地图软件", Toast.LENGTH_SHORT).show();
		}
	}

}
